﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AusProject.OrderComponents;

namespace AusProject
{
    public partial class FormMainDesigner : Form
    {
        public FormMainDesigner()
        {
            InitializeComponent();
        }

        private void FormMainDesigner_Load(object sender, EventArgs e)
        {
            LoadComponentTypeList();
            
        }


        #region Manage ComponentTypes

        private void LoadComponentTypeList()
        {
            lstComponentTypes.Items.Clear();

            //TODO: Load from DB
            OrderComponentInfo ocInfo;
            ocInfo = new OrderComponentInfo(1, "CABINETS", 600, new ComponentColumnInfo[6] { new ComponentColumnInfo(1, "", false, 2), new ComponentColumnInfo(2, "", false, 3), new ComponentColumnInfo(3, "", false, 1.5), new ComponentColumnInfo(4, "", false, 1), new ComponentColumnInfo(5, "", false, 1.5), new ComponentColumnInfo(6, "", false, 1) });
            lstComponentTypes.Items.Add(ocInfo);
            ocInfo = new OrderComponentInfo(2, "BENCHTOPS", 600, new ComponentColumnInfo[6] { new ComponentColumnInfo(7,"", false, 2), new ComponentColumnInfo(8,"", false, 3), new ComponentColumnInfo(9,"", false, 1.5), new ComponentColumnInfo(10,"", false, 1), new ComponentColumnInfo(11,"", false, 1.5), new ComponentColumnInfo(12, "", false, 1) });
            lstComponentTypes.Items.Add(ocInfo);
            ocInfo = new OrderComponentInfo(3, "BOARDS", 600, new ComponentColumnInfo[8] { new ComponentColumnInfo(13, "", false, 2), new ComponentColumnInfo(14, "Qty", true, 1), new ComponentColumnInfo(15, "ID#", true, 1), new ComponentColumnInfo(16, "Type", true, 1), new ComponentColumnInfo(17, "", false, 1.5), new ComponentColumnInfo(18, "", false, 1), new ComponentColumnInfo(19, "", false, 1.5), new ComponentColumnInfo(20, "", false, 1) });
            lstComponentTypes.Items.Add(ocInfo);

            if (lstComponentTypes.Items.Count > 0)
            {
                lstComponentTypes.SelectedIndex = 0;
            }
            else
            {
                lstSuppliers.Items.Clear();
                lstSuppliers_SelectedIndexChanged(null, null);
                btnAddSupplier.Enabled = false;
            }
        }

        private void lstComponentTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstComponentTypes.SelectedIndex >= 0)
            {
                btnEditComponentType.Enabled = true;
                btnDelComponentType.Enabled = true;
                LoadSupplierList(((OrderComponentInfo)lstComponentTypes.SelectedItem).Id);

                btnAddSupplier.Enabled = true;
            }
            else
            {
                btnEditComponentType.Enabled = false;
                btnDelComponentType.Enabled = false;
                lstSuppliers.Items.Clear();
                lstSuppliers_SelectedIndexChanged(null, null);

                btnAddSupplier.Enabled = false;
            }
        }

        private void lstComponentTypes_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = lstComponentTypes.IndexFromPoint(e.Location);
            if (idx >= 0)
            {
                lstComponentTypes.SelectedIndex = idx;
                btnEditComponentType_Click(null, null);
            }
        }

        private void btnCreateComponentType_Click(object sender, EventArgs e)
        {
            FormComponentTypeDesigner designer = new FormComponentTypeDesigner();
            designer.ShowDialog();
            if (designer.Accepted)
            {
                OrderComponentInfo ocInfo = designer.GetComponentInfo();

                lstComponentTypes.Items.Add(ocInfo);
                lstComponentTypes.SelectedIndex = lstComponentTypes.Items.Count - 1;

                //TODO: Sync to DB
                ocInfo.Id = 4;

                LoadSupplierList(ocInfo.Id);
                lstSuppliers_SelectedIndexChanged(null, null);
            }
        }
        
        private void btnEditComponentType_Click(object sender, EventArgs e)
        {
            FormComponentTypeDesigner designer = new FormComponentTypeDesigner();
            designer.ImportComponent((OrderComponentInfo)lstComponentTypes.SelectedItem);
            designer.ShowDialog();
            if (designer.Accepted)
            {
                int idx = lstComponentTypes.SelectedIndex;
                lstComponentTypes.Items.RemoveAt(idx);
                lstComponentTypes.Items.Insert(idx, designer.GetComponentInfo());
                lstComponentTypes.SelectedIndex = idx;

                //TODO: Sync to DB
            }
        }

        private void btnDelComponentType_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete this component and all associated order forms?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                lstComponentTypes.Items.RemoveAt(lstComponentTypes.SelectedIndex);

                //TODO: Sync to DB
            }
        }

        #endregion Manage ComponentTypes


        #region Manage Suppliers

        private void LoadSupplierList(int componentID)
        {
            lstSuppliers.Items.Clear();

            SupplierInfo si;

            if (componentID < 2)
            {
                si = new SupplierInfo(1, "Laminex");
                lstSuppliers.Items.Add(si);
            }

            if (componentID < 3)
            {
                si = new SupplierInfo(2, "Stone");
                lstSuppliers.Items.Add(si);
            }

            if (componentID < 4)
            {
                si = new SupplierInfo(3, "DX");
                lstSuppliers.Items.Add(si);
            }

            if (lstSuppliers.Items.Count > 0)
            {
                lstSuppliers.SelectedIndex = 0;
            }
            else
            {
                lstSupplierOrderForms.Items.Clear();
                lstSupplierOrderForms_SelectedIndexChanged(null, null);
                btnCreateSupplierOrderForm.Enabled = false;
            }
        }

        private void lstSuppliers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSuppliers.SelectedIndex >= 0)
            {
                btnRemSupplier.Enabled = true;
                LoadSupplierOrderFormList(((OrderComponentInfo)lstComponentTypes.SelectedItem).Id, ((SupplierInfo)lstSuppliers.SelectedItem).Id);
                btnCreateSupplierOrderForm.Enabled = true;
            }
            else
            {
                btnRemSupplier.Enabled = false;
                lstSupplierOrderForms.Items.Clear();
                lstSupplierOrderForms_SelectedIndexChanged(null, null);

                btnCreateSupplierOrderForm.Enabled = false;
            }
        }

        private void btnAddSupplier_Click(object sender, EventArgs e)
        {
            SupplierInfo si;

            si = new SupplierInfo(4, "NEW SUPPLIER");
            lstSuppliers.Items.Add(si);

            lstSuppliers.SelectedIndex = lstSuppliers.Items.Count - 1;
        }

        private void btnRemSupplier_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Remove this supplier and all associated order forms?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                lstSuppliers.Items.RemoveAt(lstSuppliers.SelectedIndex);

                //TODO: Sync to DB
            }
        }

        #endregion Manage Suppliers


        #region Manage Supplier Order Forms

        private void LoadSupplierOrderFormList(int componentID, int supplierID)
        {
            lstSupplierOrderForms.Items.Clear();

            //TODO: Load form DB
            List<OrderDataControls.DataBoxInfo> listOfDataBox = new List<OrderDataControls.DataBoxInfo>();
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(1, OrderDataControls.DataBoxType.TextDataBox, "Supplier", true, OrderDataControls.DataControlBorderMode.Full, "Laminex", 20, 20, 200, 30, null, 13, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(2, OrderDataControls.DataBoxType.NumberDataBox, "Quantity", true, OrderDataControls.DataControlBorderMode.Full, "5", 20, 60, 200, 30, null, 14, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(3, OrderDataControls.DataBoxType.TextDataBox, "ID#", true, OrderDataControls.DataControlBorderMode.Full, "00789", 20, 100, 200, 30, null, 15, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(4, OrderDataControls.DataBoxType.TextDataBox, "Type", true, OrderDataControls.DataControlBorderMode.Full, "Rank#A", 20, 140, 200, 30, null, 16, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));

            SupplierOrderFormInfo formInfo = new SupplierOrderFormInfo("Dat hang 1", 500, 250, "", listOfDataBox);
            lstSupplierOrderForms.Items.Add(formInfo);

            if (lstSupplierOrderForms.Items.Count > 0)
            {
                lstSupplierOrderForms.SelectedIndex = 0;
            }
            else
            {
                btnEditSupplierOrderForm.Enabled = false;
                btnDelSupplierOrderForm.Enabled = false;
            }
        }

        private void lstSupplierOrderForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSupplierOrderForms.SelectedIndex >= 0)
            {
                btnEditSupplierOrderForm.Enabled = true;
                btnDelSupplierOrderForm.Enabled = true;
            }
            else
            {
                btnEditSupplierOrderForm.Enabled = false;
                btnDelSupplierOrderForm.Enabled = false;
            }
        }

        private void lstSupplierOrderForms_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = lstSupplierOrderForms.IndexFromPoint(e.Location);
            if (idx >= 0)
            {
                lstSupplierOrderForms.SelectedIndex = idx;
                btnEditSupplierOrderForm_Click(null, null);
            }
        }

        private void btnCreateSupplierOrderForm_Click(object sender, EventArgs e)
        {
            FormSupplierOrderDesigner designer = new FormSupplierOrderDesigner();
            designer.SetOrderComponentType((OrderComponentInfo)lstComponentTypes.SelectedItem);
            this.Hide();
            designer.ShowDialog();
            this.Show();

            if (designer.Accepted)
            {
                lstSupplierOrderForms.Items.Add(designer.ExportFormInfo());
                lstSupplierOrderForms.SelectedIndex = lstSupplierOrderForms.Items.Count - 1;
            }
        }

        private void btnEditSupplierOrderForm_Click(object sender, EventArgs e)
        {
            FormSupplierOrderDesigner designer = new FormSupplierOrderDesigner();
            designer.SetOrderComponentType((OrderComponentInfo)lstComponentTypes.SelectedItem);
            designer.ImportFormInfo((SupplierOrderFormInfo)lstSupplierOrderForms.SelectedItem);

            this.Hide();
            designer.ShowDialog();
            this.Show();

            if (designer.Accepted)
            {
                int idx = lstSupplierOrderForms.SelectedIndex;
                lstSupplierOrderForms.Items.RemoveAt(idx);
                lstSupplierOrderForms.Items.Insert(idx, designer.ExportFormInfo());
                lstSupplierOrderForms.SelectedIndex = idx;

                //TODO: Sync to DB
            }
        }

        private void btnDelSupplierOrderForm_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete this supplier order form?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                lstSupplierOrderForms.Items.RemoveAt(lstSupplierOrderForms.SelectedIndex);

                //TODO: Sync to DB
            }
        }

        #endregion Manage Supplier Order Forms


        #region Manage Order Summary Forms

        private void LoadOrderSummaryFormList()
        {
            lstOrderSummaryForms.Items.Clear();
        }

        private void lstOrderSummaryForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstOrderSummaryForms.SelectedIndex >= 0)
            {
                btnEditOrderSummaryForm.Enabled = true;
                btnDelOrderSummaryForm.Enabled = true;
            }
            else
            {
                btnEditOrderSummaryForm.Enabled = false;
                btnDelOrderSummaryForm.Enabled = false;
            }
        }

        private void lstOrderSummaryForms_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = lstOrderSummaryForms.IndexFromPoint(e.Location);
            if (idx >= 0)
            {
                lstOrderSummaryForms.SelectedIndex = idx;
                btnEditOrderSummaryForm_Click(null, null);
            }
        }

        private void btnCreateOrderSummaryForm_Click(object sender, EventArgs e)
        {
            FormOrderSummaryDesigner designer = new FormOrderSummaryDesigner();
            this.Hide();
            designer.ShowDialog();
            this.Show();

            if (designer.Accepted)
            {
                lstOrderSummaryForms.Items.Add(designer.ExportFormInfo());
                lstOrderSummaryForms.SelectedIndex = lstOrderSummaryForms.Items.Count - 1;
            }
        }

        private void btnEditOrderSummaryForm_Click(object sender, EventArgs e)
        {
            FormOrderSummaryDesigner designer = new FormOrderSummaryDesigner();
            designer.ImportFormInfo((OrderSummaryFormInfo)lstOrderSummaryForms.SelectedItem);

            this.Hide();
            designer.ShowDialog();
            this.Show();

            if (designer.Accepted)
            {
                int idx = lstOrderSummaryForms.SelectedIndex;
                lstOrderSummaryForms.Items.RemoveAt(idx);
                lstOrderSummaryForms.Items.Insert(idx, designer.ExportFormInfo());
                lstOrderSummaryForms.SelectedIndex = idx;

                //TODO: Sync to DB
            }
        }

        private void btnDelOrderSummaryForm_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete this order summary form?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                lstOrderSummaryForms.Items.RemoveAt(lstOrderSummaryForms.SelectedIndex);

                //TODO: Sync to DB
            }
        }

        #endregion Manage Order Summary Forms


        


    }
}